package com.gjm.ai.controller;


import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import javax.print.attribute.standard.PresentationDirection;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.util.List;
import java.util.stream.StreamSupport;

@RequestMapping("/ds")
@RestController
public class DeepSeekController {

    @Autowired
    private OpenAiChatModel chatModel;

//    @RequestMapping("/chat")
//    public String chat(String prompt) {
//
//        return chatModel.call(prompt);
//    }

    @GetMapping("/chat")
    public String chat(String message) {
        Prompt prompt = new Prompt(message);

        ChatResponse response = chatModel.call(prompt);
        return response.getResult().getOutput().getText();
    }

    @GetMapping("/role")
    public String role(String message) {

        //设置系统提示词
        SystemMessage systemMessage = new SystemMessage("你叫小高，是一名学生，擅长数学等学科");

        //设置用户提示词
        UserMessage userMessage = new UserMessage(message);

        Prompt prompt = new Prompt(List.of(systemMessage, userMessage));

        ChatResponse response = chatModel.call(prompt);
        return response.getResult().getOutput().getText();
    }

    @GetMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String message) {
        Prompt prompt = new Prompt(message);
        Flux<ChatResponse> stream = chatModel.stream(prompt);
        return stream.map(chatResponse -> {
            return chatResponse.getResult().getOutput().getText();
        });
    }
}
